ggplot2Note: There are often multiple ways to answer each question.
Load the ggplot2 and fueleconomy packages, as well as the vehicles dataset.
library(ggplot2)
library(fueleconomy)
data(vehicles)
hwy vs. cty.ggplot(vehicles, aes(x = cty, y = hwy)) +
geom_point()
cyl column to a factor.vehicles$cyl <- factor(vehicles$cyl)
cyl value. Also, change the color scale to “YlOrRd”.ggplot(vehicles, aes(x = cty, y = hwy, col = cyl)) +
geom_point() +
scale_color_brewer(palette = "YlOrRd")
## Warning: Removed 58 rows containing missing values (geom_point).
Notice how the NAs got removed!
alpha = 0.1.ggplot(vehicles, aes(x = cty, y = hwy, col = cyl)) +
geom_point(alpha = 0.1)
year.ggplot(vehicles, aes(x = year)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
year with just 5 bins.ggplot(vehicles, aes(x = year)) +
geom_histogram(bins = 5)
cyl, make a violin plot of hwy values.ggplot(vehicles, aes(x = cyl, y = hwy)) +
geom_violin()
cyl, make a boxplot of hwy values.ggplot(vehicles, aes(x = cyl, y = hwy)) +
geom_boxplot()
fuel there are in the dataset. (Hint: Use the geom_bar geom.)ggplot(vehicles, aes(x = fuel)) +
geom_bar()
coord_flip() layer to the previous plot to make a horizontal barplot.ggplot(vehicles, aes(x = fuel)) +
geom_bar() +
coord_flip()